package com.tianbing.adlottery.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.fengniu.ejb.business.ICouponBusiness;
import com.fengniu.ejb.business.ISiteBusiness;
import com.fengniu.ejb.business.IUserBusiness;
import com.fengniu.ejb.dao.IContentDao;
import com.fengniu.ejb.dao.ISiteDao;
import com.fengniu.ejb.entity.Category;
import com.fengniu.ejb.entity.Content;
import com.fengniu.ejb.entity.Site;
import com.fengniu.ejb.entity.Site2Content;
import com.fengniu.ejb.entity.Site2Site;
import com.tianbing.adlottery.global.IConstants;
import com.tianbing.adlottery.helper.SkinUtils;
import com.tianbing.adlottery.util.UtilDataGroup;

/**
 * MultiActionController for the image list/upload UI.
 * 
 * @author Conan Chen
 * @since 2006.12.15
 */
public class StoreController extends MultiActionController {
	Logger logger = Logger.getLogger(StoreController.class.getName());

	private String storeDetailView;

	private String ajaxGetStoreDetailView;

	private String listStoreCouponView;

	private String storeCouponDetailView;

	private String ajaxListStoreSellProductView;

	private int productPageSize = 16;

	private ISiteDao siteDao;

	private IContentDao contentDao;

	private SkinUtils skinUtils;

	private IUserBusiness userBusiness;

	private ISiteBusiness siteBusiness;

	private ICouponBusiness couponBusiness;

	@SuppressWarnings("unchecked")
	public ModelAndView viewStoreDetail(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String method = ServletRequestUtils.getStringParameter(request, "method", null);
		if (method != null && method.equals("ajax")) {
			return ajaxListStoreSellProduct(request, response);
		} else {
			ModelAndView mv = new ModelAndView(storeDetailView);
			Map model = mv.getModel();
			// get site store detail
			String sgid = ServletRequestUtils.getStringParameter(request, "sgid", null);
			Long ssid = ServletRequestUtils.getLongParameter(request, "ssid", -1);

			Site siteDetail = siteDao.findSiteBySysId(ssid);
			model.put("siteDetail", siteDetail);
			model.put("sgid", sgid);
			return mv;
		}
	}

	public ModelAndView ajaxGetStoreDetail(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(ajaxGetStoreDetailView);
		Map model = mv.getModel();

		// get site store detail
		Long ssid = ServletRequestUtils.getLongParameter(request, "ssid", -1);
		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		Integer pgsize = 20;// ServletRequestUtils.getIntParameter(request,
		// "pgsize", 30);
		String odby = ServletRequestUtils.getStringParameter(request, "odby", "");

		// get site store detail
		Site siteDetail = siteDao.findSiteBySysId(ssid);
		model.put("siteDetail", siteDetail);

		if (siteDetail != null) {
			List<Site2Site> pageSections = siteBusiness.getSitePageSections(ssid);
			model.put("topSection", getPageSection(pageSections, "_TOP"));
			model.put("leftSection", getPageSection(pageSections, "_LEFT"));

			// get site's cateogry trail
			List<Category> primarySiteCategoryTrail = siteBusiness.getSiteLongestCategoryTrail(ssid,
					IConstants.CategoryID.AREA_TOP.getId());
			if (primarySiteCategoryTrail != null && primarySiteCategoryTrail.size() > 0) {
				model.put("primarySiteCategoryTrail", primarySiteCategoryTrail);
				Category siteCategory = primarySiteCategoryTrail.get(primarySiteCategoryTrail.size() - 1);
				if (siteCategory != null) {
					model.put("siteCategory", siteCategory);
				}
			}

			// get site2content for this site

			Map s2cMap = siteBusiness.getStoreSellProducts(-1, ssid, null, pgno, pgsize);
			List store2ContentList = (List) s2cMap.get("site2ContentList");
			Long totalCount = (Long) s2cMap.get("totalCount");
			List site2ContentGroupList = UtilDataGroup.makeGroups(store2ContentList, 0, 4);

			model.put("site2ContentGroupList", site2ContentGroupList);
			model = skinUtils.preparePageFooter(request, store2ContentList, totalCount, pgno, pgsize, model);
			model.put("pagefooterparas", "ssid=" + ssid);

			// get promotion coupon detail
			Long csid = ServletRequestUtils.getLongParameter(request, "csid", -1);
			if (csid > 0) {
				Content contentDetail = contentDao.findContentBySysId(csid);
				model.put("contentDetail", contentDetail);
			}

			// get recommended site 2 this coupon
			if (csid > -1) {
				Site2Content recommendedSite2Content = siteBusiness.findStoreSellProduct(csid, ssid);
				model.put("recommendedSite2Content", recommendedSite2Content);
			}

			// User2Site storeServant = userBusiness.findStoreServant(ssid);
			// model.put("storeServant", storeServant);
			// prepare output data map for view
			model.put("referurl", request.getRequestURL().toString() + "?" + request.getQueryString());
		}
		return mv;
	}

	/**
	 * list page of Category2Product by ajax
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ModelAndView ajaxListStoreSellProduct(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		ModelAndView mv = new ModelAndView(ajaxListStoreSellProductView);
		Map model = mv.getModel();

		Long ssid = ServletRequestUtils.getLongParameter(request, "ssid", -1);
		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		Integer pgsize = productPageSize;

		Map s2cMap = siteBusiness.getStoreSellProducts(-1, ssid, null, pgno, pgsize);
		List store2ContentList = (List) s2cMap.get("site2ContentList");
		List site2ContentGroupList = UtilDataGroup.makeGroups(store2ContentList, 0, 4);

		// prepare output data map for view
		model.put("pgno", pgno);
		model.put("site2ContentGroupList", site2ContentGroupList);
		return mv;
	}

	@SuppressWarnings("unchecked")
	public ModelAndView listStoreCoupon(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(listStoreCouponView);
		Map model = mv.getModel();
		// get site store detail
		Long ssid = ServletRequestUtils.getLongParameter(request, "ssid", -1);

		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		Integer pgsize = 30;// ServletRequestUtils.getIntParameter(request,
		// "pgsize", 30);

		Site siteDetail = siteDao.findSiteBySysId(ssid);
		Map<String, Object> s2cMap = couponBusiness.getCoupons(ssid, pgno, pgsize);

		List<Site2Content> site2ContentList = (List) s2cMap.get("site2ContentList");
		Long totalCount = (Long) s2cMap.get("totalCount");

		List site2ContentGroupList = UtilDataGroup.makeGroups(site2ContentList, 0, 4);

		model.put("siteDetail", siteDetail);
		model.put("site2ContentGroupList", site2ContentGroupList);

		model = skinUtils.preparePageFooter(request, site2ContentList, totalCount, pgno, pgsize, model);

		return mv;
	}

	@SuppressWarnings("unchecked")
	public ModelAndView viewStoreCouponDetail(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ModelAndView mv = new ModelAndView(storeCouponDetailView);
		Map model = mv.getModel();
		Long ssid = ServletRequestUtils.getLongParameter(request, "ssid", -1);

		// get site store detail
		Long csid = ServletRequestUtils.getLongParameter(request, "csid", -1);
		if (csid > 0) {
			Site2Content site2Content = couponBusiness.findCoupon(csid, ssid);
			model.put("site2Content", site2Content);

			Map<String, Object> s2cMap = couponBusiness.getCoupons(ssid, 0, 30);
			List<Site2Content> site2ContentList = (List) s2cMap.get("site2ContentList");
			model.put("site2ContentList", site2ContentList);
		}
		return mv;
	}

	public void setStoreDetailView(String homeView) {
		this.storeDetailView = homeView;
	}

	public void setSiteDao(ISiteDao siteDao) {
		this.siteDao = siteDao;
	}

	public void setSkinUtils(SkinUtils skinUtils) {
		this.skinUtils = skinUtils;
	}

	public void setContentDao(IContentDao contentDao) {
		this.contentDao = contentDao;
	}

	public void setLogger(Logger logger) {
		this.logger = logger;
	}

	public void setUserBusiness(IUserBusiness userBusiness) {
		this.userBusiness = userBusiness;
	}

	public void setListStoreCouponView(String listStoreCouponView) {
		this.listStoreCouponView = listStoreCouponView;
	}

	public void setStoreCouponDetailView(String storeCouponDetailView) {
		this.storeCouponDetailView = storeCouponDetailView;
	}

	public void setSiteBusiness(ISiteBusiness siteBusiness) {
		this.siteBusiness = siteBusiness;
	}

	public void setAjaxListStoreSellProductView(String ajaxListStoreSellProductView) {
		this.ajaxListStoreSellProductView = ajaxListStoreSellProductView;
	}

	public void setProductPageSize(int productPageSize) {
		this.productPageSize = productPageSize;
	}

	public void setAjaxGetStoreDetailView(String ajaxGetStoreDetailView) {
		this.ajaxGetStoreDetailView = ajaxGetStoreDetailView;
	}

	public void setCouponBusiness(ICouponBusiness couponBusiness) {
		this.couponBusiness = couponBusiness;
	}

	private Site2Site getPageSection(List<Site2Site> pageSections, String sectionName) {
		if (pageSections != null && pageSections.size() > 0) {
			for (int i = 0; i < pageSections.size(); i++) {
				Site2Site s2s = pageSections.get(i);
				if (s2s.getName().equals(sectionName)) {
					return s2s;
				}
			}
		}
		return null;
	}
}
